BatchMove = {

	data : {},
	AutoRunCount : 0

};
BatchMoveData = {

};
//共享文件目录树
function folderTree2(){
	$.ajax({
		type: "POST",
		url: C.domain+"/share/dir/index",
		dataType:"jsonp",
		success: function(data){
			if(data.code == 0){
				var x;
				for (x in data.data){
                    var list=[];
                    if(data.data[x].has_child==true){
                        list[x]='<li><div class="treeview-node2" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[x].pkid + '">'+ data.data[x].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                    }else{
                        list[x]='<li><div class="treeview-node2 treenode-empty" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[x].pkid + '">'+ data.data[x].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                    }
                    $('.treeview-root-content').append(list[x]);
				}
			}else{
				errorCode(data.code,data.msg);
			}
		},
	})
}
//验目标文件是否有读写权限
function movetoRight(id,type,operate){
    right=false;
    $.ajax({
        type: "POST",
        async: false,
        url:C.domain+"/share/index/get_operate",
        dataType:"jsonp",
        data: {
            "share_id":id,
            "share_type":type,
            "want_operate":operate,
            },
        success: function(data){
            if(data.code == 0){
                if(data.data.has_operate==true){
                    right=true;
                    return right;
                }
                else{
                    $('#myModal').modal('hide');    
                    //Tips("目标文件夹没有权限") 
                    right=false;
                    return right;    
                }
            }
            else{
                $('#myModal').modal('hide');
                errorCode(data.code,data.msg)
                right=false;
                return right;   
            }
        },
    })
            
} 
//展开文件夹
function treeviewnode(_this){
    $(".treeview-node,.treeview-node2,.treeview-root").removeClass("treeview-node-on");
    _this.addClass("treeview-node-on");
    if(_this.hasClass("treenode-empty")){
        }else{
            _this.children().find(".icon-operate").toggleClass("minus");
            _this.next().toggleClass("treeview-collapse");		
            dir_id =_this.find(".treeview-txt").attr("dir_id");
            $.ajax({
                type: "POST",
                url: C.domain+"/disk/dir/index",
                dataType:"jsonp",
                data: {"dir_id":dir_id},
                success: function(data){
                    if(data.code == 0){
                        
                        var treeChild= $(".treeview-txt[dir_id='"+dir_id+"']").closest(".treeview-node").next(".treeview-content");
                        treeChild.html('');
                        var y;
                        for (y in data.data){
                        var listi=[];
                        if(data.data[y].has_child==true){
                            listi[y]='<li><div class="treeview-node" onclick="treeviewnode($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[y].pkid + '">'+ data.data[y].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                        }else{
                            listi[y]='<li><div class="treeview-node treenode-empty" onclick="treeviewnode($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[y].pkid + '">'+ data.data[y].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                        }
                         
                    treeChild.append(listi[y]);
                    }
                }else{
                    errorCode(data.code,data.msg);
                }
            },
        });
    }
}
//展开文件夹
function treeviewnode2(_this){
  $(".treeview-node,.treeview-node2,.treeview-root").removeClass("treeview-node-on");
    _this.addClass("treeview-node-on");
    if(_this.hasClass("treenode-empty")){
        }else{
            _this.children().find(".icon-operate").toggleClass("minus");
            _this.next().toggleClass("treeview-collapse");		
            dir_id =_this.find(".treeview-txt").attr("dir_id");
            $.ajax({
                type: "POST",
                url: C.domain+"/share/dir/index",
                dataType:"jsonp",
                data: {"dir_id":dir_id},
                success: function(data){
                    if(data.code == 0){
                        var treeChild= $(".treeview-txt[dir_id='"+dir_id+"']").closest(".treeview-node2").next(".treeview-content");
                        treeChild.html('');
                        var y;
                        for (y in data.data){
                        var listi=[];
                        if(data.data[y].has_child==true){
                            listi[y]='<li><div class="treeview-node2" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[y].pkid + '">'+ data.data[y].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                        }else{
                            listi[y]='<li><div class="treeview-node2 treenode-empty" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[y].pkid + '">'+ data.data[y].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                        }
                    treeChild.append(listi[y]);
                    }
                }else{
                    errorCode(data.code,data.msg);
                }
            },
        });
    }
}
var testchild=true;
var rootFolder;
var sharetitle;
var newnameContent='<div class="new-name"><input type="text" value="" class="box"><span class="sure"></span><span class="cancel"></span></div>';
var renameContent='<div class="change-name"><input type="text" value="" class="box"><span class="sure"></span><span class="cancel"></span></div>';
var treeContent3 = '<div class="file-tree-container"><ul class="treeview"><li><div class="treeview-node  treeview-root treeview-node" ><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">我的文件</span></span></div><ul class="treeview treeview-root-content1 treeview-content treeview-collapse"></ul></li><li><div class="treeview-node  treeview-root treeview-noden" onclick="treeviewnode()"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">共享文件</span></span></div><ul class="treeview treeview-root-content2 treeview-content  treeview-collapse"></ul></li></ul></div>';
var pwContent='<div style="text-align: center"><input type="text" value="" class="box box_pw"><p style="text-align:center;color:red" class="pwerror"></p></div>';
var modalFooter ='<button type="button" class="btn btn-primary tree_confirm">确定</button><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>';
var txt,f1,f2,remnant,foot1,foot2,d;
var owner=false;
var right;
function foot(data){
if (data >= 1) {
	foot1='<div class="batch" style="padding-bottom:15px"><input name="share" type="checkbox" value="public"/> 为其余'+data+'个重名文件都执行此操作</div><button type="button" class="btn btn-primary replace" style="padding:0 20px" disabled="disabled">替换文件</button><button type="button" class="btn btn-primary keep" style="padding:0 20px">保留两个文件</button><button type="button" class="btn btn-default cancelbtn" data-dismiss="modal" style="padding:0 20px">取消</button><div class="red">提示：当前权限不可替换操作</div>';
	foot2='<div class="batch" style="padding-bottom:15px"><input name="share" type="checkbox" value="public"/> 为其余'+data+'个重名文件夹都执行此操作</div><button type="button" class="btn btn-primary jump" style="padding:0 20px">跳过</button><button type="button" class="btn btn-primary keep" style="padding:0 20px">保留两个文件夹</button><button type="button" class="btn btn-default cancelbtn" data-dismiss="modal" style="padding:0 20px">取消</button>';
    foot3='<div class="batch" style="padding-bottom:15px"><input name="share" type="checkbox" value="public"/> 为其余'+data+'个重名文件都执行此操作</div><button type="button" class="btn btn-primary replace" style="padding:0 20px">替换文件</button><button type="button" class="btn btn-primary keep" style="padding:0 20px">保留两个文件</button><button type="button" class="btn btn-default cancelbtn" data-dismiss="modal" style="padding:0 20px">取消</button>';
	}else if(data < 1){
	foot3='<button type="button" class="btn btn-primary replace" style="padding:0 20px">替换文件</button><button type="button" class="btn btn-primary keep" style="padding:0 20px">保留两个文件</button><button type="button" class="btn btn-default cancelbtn" data-dismiss="modal" style="padding:0 20px">取消</button>';
	foot2='<button type="button" class="btn btn-primary keep" style="padding:0 20px">保留两个文件夹</button><button type="button" class="btn btn-default cancelbtn" data-dismiss="modal" style="padding:0 20px">取消</button>';
    foot1='<button type="button" class="btn btn-primary replace" style="padding:0 20px" disabled="disabled">替换文件</button><button type="button" class="btn btn-primary keep" style="padding:0 20px">保留两个文件</button><button type="button" class="btn btn-default cancelbtn" data-dismiss="modal" style="padding:0 20px">取消</button><div class="red">提示：当前权限不可替换操作</div>';
	}
return foot1,foot2;
}
var catagoryHeader='<div class="col-item check"><span class="check-icon" node-type="check"></span></div><ul class="list-cols"><li style="width:50%;" class="col" data-key="name"><span class="text">文件名</span><span class="order-icon"></span></li><li style="width:25%;"><span class="path">路径</span></li><li style="width:10%;" class="col" data-key="size"><span class="text">大小</span><span class="order-icon"></span></li><li style="width:15%;" class="col descend" data-key="date"><span class="text">修改日期</span><span class="order-icon"></span></li></ul><div class="list-header-operatearea"><span class="count-tips">已选中<span id="select-sum"></span>个文件/文件夹</span><div class="list-header-operate"><a href="javascript:void(0);"  class="g-button" id="share"><em title="分享" class="icon icon-share-gray"></em>分享</a><a href="javascript:void(0);" class="g-button" id="downloadBtn"><em title="下载" class="icon icon-download-gray"></em>下载</a><a href="javascript:void(0);" class="g-button" id="alldelete"><em title="删除" class="icon icon-delete"></em>删除</a><a href="javascript:void(0);" class="g-button re-name" >重命名</a><a href="javascript:void(0);"  class="g-button copy-to" style="">复制到</a><a href="javascript:void(0);" class="g-button move-to">移动到</a></div></div>';
var currPage =1;
var flag,fl,c,d,can;
var noRepeatFile;
var noRepeatFolder;
var repeatFile;
var repeatFolder;
var toFile;
var toDir;
var go=false;
//第一次加载内容
 function firstLoad(types){
	id=$(".right-col").attr("dir_id");	
	//getList(1,url,template,id,types);
	params={
			page:1,
			dir_id: id,
			limit: 100,
			types:types
			};
	getList(url1,params,listTemplate1,id);
  }
 //剩余空间
function space(){    
$.ajax({
		type: "POST",
		url: C.domain+"/disk/index/space",
		dataType:"jsonp",
		success: function(data){
			if(data.code == 0){
				var space =data.data.use_format;
				$(".remainingSpace .bold").html(space);
				remnant=FormatSize(data.data.total-data.data.use);				
				var remain=(data.data.use/data.data.total)*100;
				var per= remain.toFixed(2)+ "%"
				$(".progress-index").css("width",per)
				return remnant;
			}else{
				errorCode(data.code,data.msg);
				}
		},
});
};
//验权
function checkFolderRight(operate){
    if($(".right-col>div:first").attr("id")=="shareDiv"){        
        if($("#main").attr("operate").indexOf(operate)>=0){
            return true;
        }else{
            //Tips("没有此操作权限")
            return false;
        }
    }else{
        return true;
    }
}
function modaltxt(data_a,data_b){
	console.log("function modaltxt(data_a,data_b)");
	console.log(data_a,data_b);
f1='<h4 style="text-align:center" id="'+data_a.pkid+'">'+txt+'的位置已经包含了同名的文件，请选择你的操作：</h4><div class="file"><div class="list-view" style="padding-top:10px;position:relative"><div>已有文件：</div><div class="img" style="left:0"><a class="filetype icon-'+data_b.suffix+'" style="top:8px;left: 0"></a></div><div style="display: block;height: 30px; line-height: 30px; text-indent: 20px;"><p title="'+ data_b.name+'" class="text" style="width:100%; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">'+ data_b.name+'</p></div><div style="line-height:24px;display: inline-block;padding-right:30px">大小：'+ data_b.file_size+'</div><div style="line-height:24px;display: inline-block;">修改时间：'+ data_b.modify+'</div></div></div><div class="file"><div class="list-view" style="padding-top:20px;position:relative"><div>正在'+txt+'的文件：</div><div class="img" style="left:0"><a class="filetype icon-'+data_a.suffix+'" style="top:8px;left: 0"></a></div><div style="display: block;height: 30px; line-height: 30px; text-indent: 20px;"><p title="'+ data_a.name+'" class="text" style="width:100%; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">'+ data_a.name+'</p></div><div style="line-height:24px;display: inline-block;padding-right:30px">大小：'+ data_a.file_size+'</div><div style="line-height:24px;display: inline-block;">修改时间：'+ data_a.modify+'</div></div></div>';
f2='<h4 style="text-align:center" id="'+data_a.pkid+'">'+txt+'的位置已经包含了同名的文件夹，请选择你的操作：</h4><div class="file"><div class="list-view" style="padding-top:10px;position:relative"><div>已有文件夹：</div><div class="img" style="left:0"><a class="filetype icon-folder" style="top:8px;left: 0"></a></div><div style="display: block;height: 30px; line-height: 30px; text-indent: 20px;"><p title="'+ data_b.name+'" class="text" style="width:100%; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">'+ data_b.name+'</p></div><div style="line-height:24px;display: inline-block;">修改时间：'+ data_b.modify+'</div></div></div><div class="file"><div class="list-view" style="padding-top:20px;position:relative"><div>正在'+txt+'的文件夹：</div><div class="img" style="left:0"><a class="filetype icon-folder" style="top:8px;left: 0"></a></div><div style="display: block;height: 30px; line-height: 30px; text-indent: 20px;"><p title="'+ data_a.name+'" class="text" style="width:100%; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">'+ data_a.name+'</p></div><div style="line-height:24px;display: inline-block;">修改时间：'+ data_a.modify+'</div></div></div>';
return f1,f2;
}
window.onload=function(){
//预览
 $(".list-wrapper").off("click").on("click",".preview",function(){
	$(".if-preview").append('<iframe  frameborder="no" style="width:100%;height:100%;border:0;position: fixed; left: 0px; top: 0px; z-index: 999;"  marginheight="0" marginwidth="0" border="0" name="if-preview" src=""></iframe>');
	 $(".outerdiv").show();
	 $(".closeiframe").show();
	 $(".outer").show();
	$(".if-preview").show();
 })
 $(".closeiframe").click(function(){
	$(".outerdiv").hide();
	 $(".closeiframe").hide();
	 $(".outer").hide();
	$(".if-preview").hide();
$(".if-preview").html("");
 })
 //列表切换----列表模式
  $(".list-switch").click(function(){ 
  $(this).parent().removeClass("grid-switched-on").addClass("list-switched-on");
  $(".list-view-container").removeClass("grid-view").addClass("list-view");
  $(".list-header ul").show();
  //listOperate()
  return false;  
  });
 //列表切换----缩略图模式 
  $(".grid-switch").click(function(){ 
  $(this).parent().removeClass("list-switched-on").addClass("grid-switched-on");
  $(".list-view-container").removeClass("list-view").addClass("grid-view");
  $(".list-header ul").hide();
	$(".thumb img").each(function(){
	drawpic($(this));
	})
  //listOperate()
  return false;
  });

//排序
$(".list-header").off("click").on("click",".col",function(){	
	var a=$(this);
	var order=$(this).attr("data-key");
	var sort;
	if(a.hasClass("ascend")){
		a.removeClass("ascend");
		a.addClass("descend");
		sort="desc";
		
	}else if(a.hasClass("descend")){
		a.removeClass("descend");
		a.addClass("ascend");
		sort="asc";
		
	}else{	
		$(".list-cols li").each(function(){
		if($(this).hasClass("ascend")){
			$(this).removeClass("ascend");
			a.addClass("ascend");
			sort="asc";
			
		}
		else if($(this).hasClass("descend")){
			$(this).removeClass("descend");
			a.addClass("descend");
			sort="desc";
			
		}
	})
	}
	setTimeout(function(){
	sortOperate(order,sort)
	},300)
})
function sortOperate(order,sort){
		$(".list-wrapper").html("");
	if($("#main").parent().attr("id")=="home"){
		id=$("#main").attr("dir_id");		
		params={
				page:1,
				limit: 100,
				dir_id:id,
				order:order,
				sort:sort
				}
		getList(url1,params,listTemplate1);
	}else if($("#main").parent().attr("id")=="search"){
		name=$(".keyword").html();		
		params={
				page:1,
				limit: 100,
				name:name,
				order:order,
				sort:sort
				};
		searchList(params)
	}else if($("#main").parent().attr("id")=="shareDiv"){
		id=$("#main").attr("dir_id");
		params={
				page:1,
				limit: 100,
				dir_id:id,
				order:order,
				sort:sort
				};
		getList(url3,params,listTemplate7);
	}
}
//移除匹配元素
function removeList(dir_id,file_id){
	var arr_file = file_id.split(',');
	var arr_dir = dir_id.split(',');	
	if(file_id.length!==0){	
        for(var i=0;i<arr_file.length;i++){
            $("[file_id="+arr_file[i]+"]").remove();
        }
	}
	if(dir_id.length!==0){	
        for(var j=0;j<arr_dir.length;j++){		
            $("[dir_id="+arr_dir[j]+"]").remove();
        }
	}
    if($(".list-selected").length==0){
        $(".list-header-operatearea").hide();
        $(".list-view .list-header ul").show();
    }
    $(".check").removeClass("checked");
    if($(".list-wrapper").html()==""){
        $(".check-icon").mask();
    }else{
        $(".check-icon").unmask();
    }
}
//查找文件ID
function findId(obj){
    var dir_ids='';
    var file_ids='';
    var fa_ids='';	
    var checked = $(".list-selected").length;
    $(".list").each(function(){
        _this =$(this);
        if(obj.closest(".list").attr("file_id") || obj.closest(".list").attr("dir_id")){
            if(typeof(obj.closest(".list").attr("file_id"))=="undefined"){
                dir_ids =obj.closest(".list").attr('dir_id');
            }else{
                file_ids = obj.closest(".list").attr('file_id');
                if($("#main").parent().attr("id")=="search" ||$("#main").parent().attr("id")=="catagory"){
                    fa_ids= obj.closest(".list").find(".file-path").attr("dir_id");
                }
            }
        }else if(checked > 0){
            if(_this.hasClass("list-selected")){
                if(typeof(_this.attr("file_id"))=="undefined"){
                    if(dir_ids ==""){
                        dir_ids += _this.attr('dir_id');
                    }
                    else{
                        dir_ids += ","+ _this.attr('dir_id');
                    }
                }else if(typeof(_this.attr("dir_id"))=="undefined"){
                    if($("#main").parent().attr("id")=="search"){
                        if(fa_ids ==""){
                            fa_ids += _this.find(".file-path").attr("dir_id");
                        }else{
                            fa_ids += ","+ _this.find(".file-path").attr("dir_id");
                        }
                    }
                    if(file_ids ==""){
                        file_ids += _this.attr('file_id');
                    }else{
                        file_ids += ","+ _this.attr('file_id');
                    }			
                }	
            }
        }
    })
    console.log([dir_ids,file_ids,fa_ids])
    return[dir_ids,file_ids,fa_ids];
}

//我的文件目录树
function folderTree(){  
	$.ajax({
		type: "POST",
		url: C.domain+"/disk/dir/index",
		dataType:"jsonp",
		success: function(data){
             
			if(data.code == 0){
               
				var x;
				for (x in data.data){
                    var list=[];
                    
                    if(data.data[x].has_child==true){
                        list[x]='<li><div class="treeview-node" onclick="treeviewnode($(this))""><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[x].pkid + '">'+ data.data[x].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                    }else{
                        list[x]='<li><div class="treeview-node treenode-empty" onclick="treeviewnode($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ data.data[x].pkid + '">'+ data.data[x].name +'</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li>';
                    }
                    
                    $('.treeview-root-content').append(list[x]);
				}
			}else{
				errorCode(data.code,data.msg);
			}
		},
	})
}


function download(obj){	
    var id = findId(obj);
    var server_id;
	id = id[1];
	if($(".right-col>div:first").attr("id")=="home"||$("[file_id='"+id+"']").parents("#my-div").length>0){
        url=C.domain+'/disk/file/file_info?file_id='+id+'';
        type="disk";
    }else{
        url=C.domain+'/share/file/file_info?file_id='+id+'';
        type="share";
    }
    $.ajax({
        type: "POST",
        async: false,
        url:url,
        dataType:"jsonp",
        data:{"file_id":id},
        success: function(data){
            if(data.code == 0){
                server_id=data.data.server_name;
                console.log(server_id);
                return server_id;                        
            }
            else{				
                errorCode(data.code,data.msg)
            }
        },
    });
    console.log(server_id);
    if(C.servers[server_id].available==true){
        server_id=server_id;
    }
    else{
        server_id=C.CenterServer;                           
    }
    if(type=="disk"){
        href=C.domain+'/disk/file/download?id='+id+'&server_name='+server_id;  
    }
    else if(type=="share"){
        href=C.domain+'/share/file/download?id='+id+'&server_name='+server_id; 
    }
    obj.attr("href",href);
	return false;
}
$(document).ready(function(){
    
 //列表操作
$(".list-wrapper").on("mouseenter",".list",function(){
    if($(".new-name").is(":visible")==false){
        $(this).addClass("list-hover");
        if($(this).parents(".list-view").length>0){
            if($(".list-selected").length <= 1){
                if(typeof($(this).attr("file_id"))=="undefined"){//判断是否文件夹
                    $(this).find(".operate").children(".icon-download").hide();		
                }
                $(this).find(".operate").show();
            }
        }
    }
 });
//$(".list-wrapper").on("mouseleave",".list,.dropdown-menu",function(){
 //   $(this).removeClass("list-hover");
  //  $(this).find(".operate").hide();
  //  $(this).parent().removeClass("open");
 //});
$(".list-wrapper").on("mouseleave", ".list,.dropdown-menu", function(){
    $(this).removeClass("list-hover");
    $(this).find(".operate").hide();
  $(this).parent().removeClass("open");
});
//复选框操作
$(".list-header").on("click",".check-icon",function(){
    if($(this).parent().hasClass("checked")==true){
        $(this).parent().removeClass("checked");		
        $(".list").each(function(j){
            $(this).removeClass("list-selected")
            $("#select-sum").html(j+1);
        })
        $(".list-view-container").removeClass("list-multselected");
        $(".list-view .list-header ul").show();
        $(".list-header-operatearea").hide();
    }else{
        $(this).parent().addClass("checked");
        $(".list-view-container").addClass("list-multselected");
        $(".list-view .list-header ul").hide();
        $(".list-header-operatearea").show();
        if($("#my-div").is(":visible")==true){
            $("#share").hide();
        }
        $(".list").each(function(j){
            $(this).addClass("list-selected");
            $("#select-sum").html(j+1);       
            if(j>0){
                $("#downloadBtn").hide();
                $(".re-name").hide();
                $("#share").hide();
            }
        })
    }
});
$(".list-group").on("click",".checkbox",function(){
    var dir;
    $(this).parent().toggleClass("list-selected");
    j= $(".list").length;
    k=($(".list-selected").length);
    if(k > 1){
        $(".list-view-container").addClass("list-multselected");
    }
    else{
        if(typeof($(".list-selected").attr("file_id"))=="undefined"){
            dir = "dir";					
        }
        else{
            dir = "file";
        }	
        $(".list-view-container").removeClass("list-multselected");
    };
    if(k==j){	
        $(".check").addClass("checked");	
    }
    else{
        $(".check").removeClass("checked");
    }
    $("#select-sum").html(k);
    operateHeader(k,dir);
    listOperate(k);
  });
function operateHeader(i,dir){
	if(i==0){
		$(".list-view .list-header ul").show();
		$(".list-header-operatearea").hide();           
	}
	else if(i==1){		
		$(".list-view .list-header ul").hide();
		$(".list-header-operatearea").show();		
		$(".re-name").show();
        $("#share").show();
        if($("#my-div").is(":visible")==true){
            $("#share").hide();
        } 
		if(dir=="dir"){			
			$("#downloadBtn").hide();
		}
		else{
			$("#downloadBtn").show();
		}
	}
	else if(i>1){
        $(".list-view .list-header ul").hide();
        $(".list-header-operatearea").show();
        $("#downloadBtn").hide();
        $(".re-name").hide();
        $("#share").hide();
	}
}
function listOperate(i){
	if(i<1){
		$(this).parent().addClass("list-hover");
	}
	else if(i==1){
		$(this).parent().addClass("list-hover");
		if($(".list-view-container").hasClass("list-view")){
			$(this).parent().find(".operate").show();
		}
	}
	else{
		$(this).parent().removeClass("list-hover");
	}
}
//复选框操作结束

//})
//验证文件名
function testName(name){
    reg =/^\s|\uff1f|\u3001|\u201c|\u201d|\?|\\|\*|<|>|\||\s$/gi;
    if(name==""){	
        Tips("文件名不能为空");
    }
    else if(name.length>255||reg.test(name)){	
        $('#myModal').modal();
        $(".modal-title").html('文件名(夹)命名不符合要求');
        $(".modal-body").html('<p>1.文件夹名称不得超过255个字符。</p><p>2.文件名中不能有下列符号：“？”、“、”、“╲”、“*”、““”、“”“、“<”、“>”、“|”。</p><p>3.文件名的开头、结尾不能使用空格。</p><p>4.文件名不区分大小写。</p>');
        $(".modal-footer").html('<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>');
        
        return false;
    }
    else{
        return true;
    }
}
//重命名
$(".list-group").on("click",".rename",function(){
   id = findId($(this));
   checkRight($(this),"rwd");
    if(right){
        noCheck();
        rename($(this));
        
    }
})
$(".list-header").on("click",".re-name",function(){
    checkRight($(this),"rwd");
    if(right){
        rename($(this));
        
    }	
})
function rename(obj){
	if(obj.parent().parent().hasClass("dropdown-menu")){
		obj=obj;
	}
	else{
		obj=$(".list-selected").find(".rename");
	}
	var oldName= obj.parents(".name").children(".text").children().text();
    obj.parents(".name").children(".text").hide();
    if(obj.closest(".list").find(".change-name").length<=0){
        if($(".change-name").show()){
            $(".change-name").next().show();
            $(".change-name").remove();
        }
        obj.parents(".name").prepend(renameContent);
    }

	$(".box").val(oldName).select();
	$(".list-view-container").addClass("list-rename");
	$(".checkbox").mask(); 
	$(".check-icon").mask();
    $(".list").off('click').on("click",".cancel",function(){		
            cancelName($(this));
        })
    $(".name").off('click').on("click",".sure",function(){        
            id = findId($(this));
            changeName($(this),id);
        })
};

function changeName(obj,id){
   if($(".right-col>div:first").attr("id")=="home" || $("#my-div").is(":visible")==true){
        if(id[1].length!==0){
            url=C.domain+"/disk/file/rename";
        }
        else{
            url=C.domain+"/disk/dir/rename";
        }
    }
    else{
        if(id[1].length!==0){
            url= C.domain+"/share/file/rename";
        }
        else{
            url= C.domain+"/share/dir/rename";
        }
    }
var name = obj.prev().val();
if(testName(name)){
	$.ajax({
		type: "POST",
		url:url,
		dataType:"jsonp",
		data:{"name":name,"file_id":id[1],"dir_id":id[0]},
		success: function(data){
			if(data.code == 0){
				$(".list-view-container").removeClass("list-rename");				
				$(".check-icon").unmask();
				$(".checkbox").unmask();
				obj.parent().next().children().html(name);					
				obj.parent().next().show();
				obj.parent().remove();
			}
            else{				
				errorCode(data.code,data.msg)
			}
		},
	});
}
}
function cancelName(obj){
	obj.parent().next().show();
	obj.parent().remove();
	$(".list-view-container").removeClass("list-rename");
	$(".checkbox").unmask(); 
	$(".check-icon").unmask();
}
//新建文件夹
$(".newfolder").click(function(){
	noCheck();
	newFolder();
	$(".list-group").scrollTop(0);
	$(".new-name").on("click",".cancel",function(){
		$(".list-view-container").removeClass("list-rename");
		$(".checkbox").unmask(); 
		$(".check-icon").unmask();
		$(this).closest(".list").remove();
	})
$(".new-name").on("click",".sure",function(){
	id = $("#main").attr("dir_id");
	if($("#main").attr("dir_id")==""){
		id = $(".right-col").attr("dir_id");
	}
	var name = $(this).prev().val();
    if($(".right-col>div:first").attr("id")=="home"){
        url=C.domain+"/disk/dir/create";
    }
    else{
        url=C.domain+"/share/dir/create";
    }
    if(testName(name)){
        $.ajax({
            type: "POST",
            url:url,
            dataType:"jsonp",
            data:{"name":name,"dir_id":id},
            success: function(data){
                if(data.code == 0){
                    $(".list-view-container").removeClass("list-rename");				
                    $(".check-icon").unmask();
                    $(".checkbox").unmask();
                    $(".list-wrapper").html("");
                    params={
                            page:1,
                            dir_id: id,
                            limit: 100
                            };
                    if($(".right-col>div:first").attr("id")=="home"){
                        getList(url1,params,listTemplate1);
                    }
                    else{
                        getList(url3,params,listTemplate7);
                    }
                }
                else{
                    errorCode(data.code,data.msg)
                }
            },
        });
    }
})

})
function newFolder(){
	if($(".new-name").is(':visible')){
		$(".new-name .cancel").trigger("click");
	}
	else if($(".change-name").is(':visible')){
		$(".change-name .cancel").trigger("click");
	};
	var newfolder= '<div class="list clear" dir_id=""> <span class="checkbox" node-type="checkbox"></span><div class="img"><i class="filetype icon-folder"></i></div><div class="name"><div class="new-name"><input type="text" value="" class="box"><span class="sure"></span><span class="cancel"></span></div><p class="text" style="display: none;"><a class="filename" href="javascript:void(0);" title="新建文件夹">新建文件夹</a></p><div class="operate"><a href="javascript:void(0);" class="g-button icon-download" style=""><span class="g-button-right"><em title="下载" class="icon icon-download-blue"></em></span></a><span class="g-dropdown-button"><a href="javascript:void(0);"  class="g-button" id="dropdownMenu1" data-toggle="dropdown"></a><ul class="menu dropdown-menu"  role="menu" data-target="#dropdownMenu1"><li><a href="javascript:void(0);" class="g-button-menu move"  data-menu-id="b-menu3">移动到</a></li><li><a href="javascript:void(0);" class="g-button-menu copy" data-menu-id="b-menu4">复制到</a></li><li><a href="javascript:void(0);" class="g-button-menu rename"  data-menu-id="b-menu5" >重命名</a></li><li><a href="javascript:void(0);" class="g-button-menu delete"  data-menu-id="b-menu6" >删除</a></li></ul></span></div></div><div class="size" data-action="enter">-</div><div class="time" data-action="enter">-</div></div>'
	$(".list-wrapper").prepend(newfolder);
	$(".box").val("新建文件夹").select();
	$(".list-view-container").addClass("list-rename");
	$(".checkbox").mask(); 
	$(".check-icon").mask();
}

//删除
function Delete(obj){
    id = findId(obj);
    var arr_file="";
    var arr_dir="";
    if(id[1].length!== 0){
        arr_file = id[1].split(',')
    };
    if(id[0].length!== 0){
        arr_dir =id[0].split(',')
    }
    idSum = arr_file.length + arr_dir.length;
    listSum = $(".list").length;
    $('#myModal').modal();
    $(".modal-title").html("确认删除");
    $(".modal-body").html('<p class="simple-txt">确认要把所选文件放入回收站吗？</p>');
    $(".modal-footer").html(modalFooter);
    $('.modal-footer').off('click').on('click','.btn-primary',function(){
        if(obj.parents("#home").length>0||obj.parents("#my-div").length>0){
           url=C.domain+"/disk/dir/batch_delete"; 
        }
        else{
           url=C.domain+"/share/dir/batch_delete";  
        }
        console.log(id[0],id[1])
        $.ajax({
            type: "POST",
            url: url,
            dataType:"jsonp",
            data: {"file_ids":id[1],"dir_ids":id[0]},
            success: function(data){
                if(data.code == 0){
                    $('#myModal').modal('hide');
                    mid=$("#main").attr("dir_id");
                    var types = $(".nav-pills").find(".active").children().attr("id");
                    params={
                        page:1,
                        dir_id: mid,
                        limit: 100,
                        types:types
                        };
                    if(data.data.err_info == ""){
                        TipsSuccess('文件删除成功')
                        firstCheck();
                        $(".list-wrapper").html('');					
                        if($("#my-div").is(":visible")==true){
                            params={
                            page:1,						
                            limit: 100,
                            name:$(".keyword").text(),
                            };
                            searchList(params);
                        }
                        else if($("#pub-div").is(":visible")==true){
                            params={
                            page:1,						
                            limit: 100,
                            name:$(".keyword").text(),
                                };
                            searchListTab(params);
                        }
                        else if($(".list-group").parents("#home").length>0){
                            getList(url1,params,listTemplate1);
                        }
                        else if($(".list-group").parents("#shareDiv").length>0){
                            getList(url3,params,listTemplate7);
                        }
                    }
                    else{
                        Tips(data.data.msg);					
                        $(".list-wrapper").html('');
                        firstCheck();
                        $(".list-wrapper").html('');					
                        if($("#my-div").is(":visible")==true){
                            params={
                            page:1,						
                            limit: 100,
                            name:$(".keyword").text(),
                                };
                            searchList(params);
                        }
                        else if($("#pub-div").is(":visible")==true){
                            params={
                            page:1,						
                            limit: 100,
                            name:$(".keyword").text(),
                                };
                            searchListTab(params);
                        }
                        else if($(".list-group").parents("#home").length>0){
                            getList(url1,params,listTemplate1);
                        }
                        else if($(".list-group").parents("#shareDiv").length>0){
                            getList(url3,params,listTemplate7);
                        }
                    }				
                }
                else{
                    errorCode(data.code,data.msg);
                }
            },
        });
    });
}
$('.list-group').on('click','.delete',function(){
    checkRight($(this),"rwd");
    if(right){
        Delete($(this));
    }
})
//批量删除
$('.list-header').on('click','#alldelete',function(){
    checkRight($(this),"rwd");
    if(right){
        Delete($(this));
    }
})
//变更所有者
$(".list-group").on("click",".icon-owner",function(){    
    var share_id,share_type;
    if($(this).closest(".list").attr("type")=="dir"){
           share_id=$(this).closest(".list").attr("dir_id");
           share_type="dir";
    }
    else if($(this).closest(".list").attr("type")=="file"){
           share_id=$(this).closest(".list").attr("file_id");
           share_type="file";
    }
    $.ajax({
        type: "POST",
        url: C.domain+"/user/auth/check",
        dataType:"jsonp",
        data:{
            "share_id":share_id,
            "share_type":share_type
        },
        success: function(data){
            if(data.code == 0){
                if(data.data.is_owner==true){  
                    ownerChange($(this));                    
                }else{                    
                    Tips("系统检测当前操作没有权限。"); 
                    return false;
                }
            }else{                                      
                errorCode(data.code,data.msg);
                return false;
            }
        }
    })
})
function ownerChange(obj){
    var grouplist;
    if($(this).closest(".list").attr("type")=="dir"){
           share_id=$(this).closest(".list").attr("dir_id");
           share_type="dir";
    }
    else if($(this).closest(".list").attr("type")=="file"){
           share_id=$(this).closest(".list").attr("file_id");
           share_type="file";
    }
    $.ajax({
        type: "POST",
        url: C.domain+"",//取用户列表地址
        dataType:"jsonp",
        success: function(data){
            if(data.code == 0){
                for(var i=0;i<data.data.user.length;i++){
                   grouplist='<option value ="'+data.data.user[i]+'">'+data.data.user[i]+'</option>'; 
                }
                $("#myModal #myModalLabel").html('变更所有者');
                $("#myModal .modal-body").html("<div class='simple-txt'>选择用户：<select>"+grouplist+"</select></div>");
                $('.modal-footer').html(modalFooter); 
                $('#myModal').modal('show');    
            }
            else{
                errorCode(data.code,data.msg);
                return false;
            }
        }
    })
	$('.modal-footer').off('click').on('click','.btn-primary',function(){
        owner=$(".simple-txt").find('option:selected').val();
        $.ajax({
            type: "POST",
            url: C.domain+"",//更改所有者地址
            dataType:"jsonp",
            data:{
                "share_id":share_id,
                "owner":owner
            },
            success: function(data){
                if(data.code == 0){
                    TipsSuccess("变更成功！") 
                }
                else{
                    errorCode(data.code,data.msg);
                    return false;
                }
            }
        }) 
    })
}
//授权
$(".list-header").on("click","#share", function(){
    share($(this));  
})
$(".list-wrapper").off("click").on("click",".icon-share",function(){
	share($(this));
})

function share(obj){
    var share_id,
    share_type;
    if(obj.parent().hasClass("list-header-operate")){
        if($(".list-selected").attr("type")=="dir"){
            share_id=$(".list-selected").attr("dir_id");
            share_type="dir";
        }
        else if($(".list-selected").attr("type")=="file"){
            share_id=$(".list-selected").attr("file_id");
            share_type="file";
        }
    }
    else{
        if(obj.closest(".list").attr("type")=="dir"){
           share_id=obj.closest(".list").attr("dir_id");
           share_type="dir";
        }
        else if(obj.closest(".list").attr("type")=="file"){
           share_id=obj.closest(".list").attr("file_id");
           share_type="file";
        }
    }
    $.ajax({
            type: "POST",
            url: C.domain+"/user/auth/check",
            dataType:"jsonp",
            data:{
                "share_id":share_id,
                "share_type":share_type
            },
            success: function(data){
                if(data.code == 0){
                    if(data.data.is_owner==true){
                        group(share_id,share_type);	 
                    }else{                    
                        Tips("不是所有者，不能授权"); 
                        return false;
                    }
                }else{                                      
                    errorCode(data.code,data.msg);
                    return false;
                    }
            }
        })
    $(".modal-content").off('click').on('click','.right_confirm',function(){
        var g=$('.group[pkid]');
        var data=[]; 
        for(var i=0;i<g.length;i++){
            var user_list={};
            var user={};
            operate='';
             for(var m=0;m<g.eq(i).find(".d_right input:checked").length;m++){
                   operate+=g.eq(i).find(".d_right input:checked").eq(m).attr("name");               
               }
            var s=g.eq(i).find('div[uid]');
            for(var j=0;j<s.length;j++){
                uidOperate='';
                uid=s.eq(j).attr("uid");
                for(var k=0;k<s.eq(j).find("input:checked").length;k++){
                   uidOperate+=s.eq(j).find("input:checked").eq(k).attr("name");
                }
                user_list[uid]=uidOperate;
            }
           data.push({
                'gid':g.eq(i).attr("pkid"),
                'operate':operate,
                'user_list':user_list,
            });
        }   
         console.log(data)
    $.ajax({
            type: "POST",
            url: C.domain+"/user/auth/seting",
            dataType:"jsonp",
            data:{
                  "share_id":share_id,
                  "share_type":share_type,
                  "auth_data":JSON.stringify(data) 
                  },
            success: function(data){
                if(data.code == 0){
                    if(data.data.exist_operate_user.length==0){
                        $('#myModal').modal('hide');
                        TipsSuccess("授权成功");	 
                    }
                    else{
                        $('#myModal').modal('hide');
                        var name='';
                        for(var i=0;i<data.data.exist_operate_user.length;i++){                        
                                 name+=$("[uid='"+data.data.exist_operate_user[i]+"']").find("span").html()+',';
                        }
                        Tips("<span>"+name+"</span>授权不一致");
                    }
                }
                else{                                      
                    errorCode(data.code,data.msg);
                }
            }
        })
    });
$(".modal-content").on("click",".allselect input[name='ar']",function(){
    //ar.click(function(){ 
        d=$(this).parent().parent().parent().find(".sub input[name='d']");
        r=$(this).parent().parent().parent().find(".sub input[name='r']");
        w=$(this).parent().parent().parent().find(".sub input[name='w']");
        if(this.checked==true){ 
            for(i=0;i<r.length;i++){         
              r[i].checked=true;
            }
          }else{
            $(this).parent().parent().parent().find("input[name='aw']")[0].checked=false;
            $(this).parent().parent().parent().find("input[name='ad']")[0].checked=false;             
            for(i=0;i<r.length;i++){         
              r[i].checked=false;
            } 
            for(i=0;i<w.length;i++){         
              w[i].checked=false;
            }
            for(i=0;i<d.length;i++){         
              d[i].checked=false;
            }
          }        
    })
     $(".modal-content").on("click",".allselect input[name='ar']",function(){
    //ar.click(function(){ 
        d=$(this).parent().parent().parent().find(".sub input[name='d']");
        r=$(this).parent().parent().parent().find(".sub input[name='r']");
        w=$(this).parent().parent().parent().find(".sub input[name='w']");
        if(this.checked==true){ 
            for(i=0;i<r.length;i++){         
              r[i].checked=true;
            }
          }else{
            $(this).parent().parent().parent().find("input[name='aw']")[0].checked=false;
            $(this).parent().parent().parent().find("input[name='ad']")[0].checked=false;             
            for(i=0;i<r.length;i++){         
              r[i].checked=false;
            } 
            for(i=0;i<w.length;i++){         
              w[i].checked=false;
            }
            for(i=0;i<d.length;i++){         
              d[i].checked=false;
            }
          }        
    })
    $(".modal-content").on("click",".allselect input[name='aw']",function(){
   //aw.click(function(){
        d=$(this).parent().parent().parent().find(".sub input[name='d']");
        r=$(this).parent().parent().parent().find(".sub input[name='r']");
        w=$(this).parent().parent().parent().find(".sub input[name='w']");
      if(this.checked==true){
        $(this).parent().parent().parent().find("input[name='ar']")[0].checked=true;
        for(i=0;i<r.length;i++){         
          r[i].checked=true;
        }
        for(i=0;i<w.length;i++){         
          w[i].checked=true;
        }
      }else{
        $(this).parent().parent().parent().find("input[name='ad']")[0].checked=false;
         for(i=0;i<w.length;i++){         
          w[i].checked=false;
        } 
        for(i=0;i<d.length;i++){         
          d[i].checked=false;
        }
      }        
    })
    $(".modal-content").on("click",".allselect input[name='ad']",function(){
    //ad.click(function(){
        d=$(this).parent().parent().parent().find(".sub input[name='d']");
        r=$(this).parent().parent().parent().find(".sub input[name='r']");
        w=$(this).parent().parent().parent().find(".sub input[name='w']");
      if(this.checked==true){
        $(this).parent().parent().parent().find("input[name='ar']")[0].checked=true;
        $(this).parent().parent().parent().find("input[name='aw']")[0].checked=true;        
        for(i=0;i<r.length;i++){         
          r[i].checked=true;
        }
        for(i=0;i<w.length;i++){         
          w[i].checked=true;
        }
        for(i=0;i<d.length;i++){         
          d[i].checked=true;
        }
      }else{          
         for(i=0;i<d.length;i++){         
          d[i].checked=false;
        } 
      }        
    })
    $(".modal-content").on("change",".sub input[name='r']",function(){
        uid=$(this).parent().parent().attr("uid");
        check=this.checked;
        r=$(this).parent().parent().parent().find("input[name='r']");
        w=$(this).parent().parent().parent().find("input[name='w']");
        d=$(this).parent().parent().parent().find("input[name='d']");
        $(".modal-body [uid='"+uid+"'] input[name='r']").each(function(){
            $(this).prop("checked",check);
            setSelectAll(r,0);       
            if(this.checked==false){
                var cancel=$(this).parent().siblings().find("input");
                for(i=0;i<cancel.length;i++){
                    cancel[i].checked=false;  
                    setSelectAll(w,1);
                    setSelectAll(d,2);
                }
           }
        })
    })
    $(".modal-content").on("change",".sub input[name='w']",function(){
        uid=$(this).parent().parent().attr("uid");
        check=this.checked;
        r=$(this).parent().parent().parent().find("input[name='r']");
        w=$(this).parent().parent().parent().find("input[name='w']");
        d=$(this).parent().parent().parent().find("input[name='d']");
        $(".modal-body [uid='"+uid+"'] input[name='w']").each(function(){
            $(this).prop("checked",check);
            setSelectAll(w,1);
           if(this.checked==true){
               $(this).parent().prev().find("input[name='r']")[0].checked=true;
                setSelectAll(r,0);           
           }else{
               var cancel=$(this).parent().next().find("input");
                for(i=0;i<cancel.length;i++){
                    cancel[i].checked=false;  
                    setSelectAll(w,1);
                    setSelectAll(d,2);
                }
           }
        })
    })
    $(".modal-content").on("change",".sub input[name='d']",function(){
        uid=$(this).parent().parent().attr("uid");
        check=this.checked;
        r=$(this).parent().parent().parent().find("input[name='r']");
        w=$(this).parent().parent().parent().find("input[name='w']");
        d=$(this).parent().parent().parent().find("input[name='d']");
        $(".modal-body [uid='"+uid+"'] input[name='d']").each(function(){
            $(this).prop("checked",check);
            setSelectAll(d,2); 
           if(this.checked==true){
                $(this).parent().siblings().find("input")[0].checked=true; 
                $(this).parent().siblings().find("input")[1].checked=true;
                setSelectAll(w,1);
                setSelectAll(r,0);
           }       
        })
    })
        $(".modal-content").on("click",".d_right input[name='r']",function(){
       if(this.checked==false){
            var cancel=$(this).parent().siblings().find("input");
            for(i=0;i<cancel.length;i++){
                cancel[i].checked=false;
            }
       }
    }) 
    $(".modal-content").on("click",".d_right input[name='w']",function(){
       if(this.checked==true){
            $(this).parent().siblings().find("input")[0].checked=true;            
        }else{
           var cancel=$(this).parent().next().find("input");
            for(i=0;i<cancel.length;i++){
                cancel[i].checked=false;                 
            }
       }
    })
    $(".modal-content").on("click",".d_right input[name='d']",function(){
       if(this.checked==true){
            $(this).parent().siblings().find("input")[0].checked=true; 
            $(this).parent().siblings().find("input")[1].checked=true;
       }
    })    
$('.modal-footer').off('click').on('click','.right_set',function(){
    $("input:checkbox").attr("disabled",false);
    $("#myModal .modal-footer").html(shareFooter);
})
}

//显示用户组
function group(share_id,share_type){
    $('#rightModal .modal-body').html('');
    var shareFooter1="",
    shareFooter2="";
    shareTitle = "授权设置";
    shareFooter ='<button type="button" class="btn btn-primary right_confirm">确定授权</button><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>';
    $("#rightModal .modal-footer").html('<button type="button" class="btn btn-primary right_confirm">确定授权</button><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>');
   // $.ajax({
	//		type: "POST",
		//	url: C.domain+"/user/auth/list",
		//	dataType:"jsonp",
		//	data:{
         //       "share_id":share_id,
          //      "share_type":share_type,
          //      },
			//success: function(data){
			//	if(data.code == 0){ 
                    var data = {
    "ulist": [
        { "pkid": "M001", "Name": "1首页", "ctime": "120-200", "modify": "120-200", "user_list":[{"name":"颠倒是","unique_id":"kennykang@xidibuy.com","nick":"kennybuy","operate":"rw"},{"name":"\u5b59\u8273\u660e","unique_id":"simonsun@xidibuy.com","nick":"simon","operate"
:""},{"name":"\u5b59\u8273\u660e","unique_id":"simonsun@xidibuy.com","nick":"simon","operate"
:""},{"name":"\u5b59\u8273\u660e","unique_id":"simonsun@xidibuy.com","nick":"simon","operate"
:""}],"child": "" },
        { "pkid": "M002", "Name": "1车辆买卖", "ctime": "120-200", "modify": "120-200","user_list":[{"name":"颠倒是","unique_id":"kennykang@xidibuy.com","nick":"kennybuy","operate":"rw"},{"name":"\u5b59\u8273\u660e","unique_id":"simonsun@xidibuy.com","nick":"simon","operate"
:""}], "child":[
            { "pkid": "M003", "Name": "2新车", "ctime": "120-200", "modify": "120-200","user_list":"", "child":[
                { "pkid": "M006", "Name": "3奥迪", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" },
                { "pkid": "M007", "Name": "别克", "ctime": "120-200", "modify": "120-200", "user_list":[{"name":"颠倒是","unique_id":"kennykang@xidibuy.com","nick":"kennybuy","operate":"rw"},{"name":"\u5b59\u8273\u660e","unique_id":"simonsun@xidibuy.com","nick":"simon","operate"
:""}], "child":[{ "pkid": "M003", "Name": "新车", "ctime": "120-200", "modify": "120-200","user_list":"","child":[
                        { "pkid": "M006", "Name": "奥迪", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" },
                        { "pkid": "M007", "Name": "别克", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" }
                        ]
                    },
                    { "pkid": "M004", "Name": "二手车", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" },
                    { "pkid": "M005", "Name": "改装车", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" }
                    ]}
                ]
            },
            { "pkid": "M004", "Name": "二手车", "ctime": "120-200", "modify": "120-200","user_list":[{"name":"颠倒是","unique_id":"kennykang@xidibuy.com","nick":"kennybuy","operate":"rw"},{"name":"\u5b59\u8273\u660e","unique_id":"simonsun@xidibuy.com","nick":"simon","operate"
:""}], "child": "" },
            { "pkid": "M005", "Name": "改装车", "ctime": "120-200", "modify": "120-200","user_list":"", "child": [
            { "pkid": "M003", "Name": "新车", "ctime": "120-200", "modify": "120-200","user_list":"", "child":[
                { "pkid": "M006", "Name": "奥迪", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" },
                { "pkid": "M007", "Name": "别克", "ctime": "120-200", "modify": "120-200","user_list":"", "child": [
                    { "pkid": "M003", "Name": "新车", "ctime": "120-200", "modify": "120-200", "user_list":"","child":[
                        { "pkid": "M006", "Name": "奥迪", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" },
                        { "pkid": "M007", "Name": "别克", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" }
                        ]
                    },
                    { "pkid": "M004", "Name": "二手车", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" },
                    { "pkid": "M005", "Name": "改装车", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" }
                    ] }
                ]
            },
            { "pkid": "M004", "Name": "二手车", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" },
            { "pkid": "M005", "Name": "改装车", "ctime": "120-200", "modify": "120-200","user_list":"", "child": "" }
        ]}
        ]
        },
        { "gid": "M006", "Name": "1宠物", "ctime": "120-200", "modify": "120-200","user_list":[{"name":"颠倒是","unique_id":"kennykang@xidibuy.com","nick":"kennybuy","operate":"rw"},{"name":"\u5b59\u8273\u660e","unique_id":"simonsun@xidibuy.com","nick":"simon","operate"
:""}], "child": "" }
    ]
    };
    
                var showlist = $('<div class="group_m"></div>');
                showallUser(data.ulist, showlist);
                $("#rightModal .modal-body").append(showlist);               
                        $(".unfold").eq(0).html('收起');
                        $(".d_right").each(function(){
                            var o=$(this).attr("operate");
                            if(o.length>=1){
                                $("input:checkbox").attr("disabled","disabled");
                                $("#rightModal .modal-footer").html('<button type="button" class="btn btn-primary right_set">授权设置</button><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>');  
                                if(o.indexOf("d")>=0){
                                    $(this).find("input:checkbox").attr("checked",'true');
                                }
                                else if(o.indexOf("w")>=0){
                                    $(this).find("input:checkbox").eq(0).attr("checked",'true');
                                    $(this).find("input:checkbox").eq(1).attr("checked",'true');
                                }
                                else if(o.indexOf("r")>=0){
                                    $(this).find("input:checkbox").eq(0).attr("checked",'true');        
                                }
                            }
                        })
                        $("[uid]").each(function(){
                          var o=$(this).attr("operate");
                          if(o.length>=1){
                            $("input:checkbox").attr("disabled","disabled");
                            $("#rightModal .modal-footer").html('<button type="button" class="btn btn-primary right_set">授权设置</button><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>');  
                            if(o.indexOf("d")>=0){
                                $(this).find("input:checkbox").attr("checked",'true');
                            }else if(o.indexOf("w")>=0){
                                $(this).find("input:checkbox").eq(0).attr("checked",'true');
                                $(this).find("input:checkbox").eq(1).attr("checked",'true');
                            }else if(o.indexOf("r")>=0){
                                $(this).find("input:checkbox").eq(0).attr("checked",'true');        
                            }
                          }
                        })
                    $("#rightModal .modal-title").html(shareTitle);    
                    $('#rightModal').modal();    
				}
                //else{
					//errorCode(data.code,data.msg);
				//}
				//},
	//}) 
//}
//授权操作开始
$(".modal-body").off("click").on("click",".unfold",function(){
    if($(this).parent().find(".subgroup").is(":visible")==true||$(this).parent().find(".group_m").is(":visible")==true){
        $(this).html("展开");
    }
    else{
        $(this).html("收起");
    }
    $(this).parent().children(".subgroup").eq(0).toggle();
    $(this).parent().children(".group_m").eq(0).toggle();
    if($(this).html()=="展开"){
        $(this).parent().find(".subgroup").hide();
        $(this).parent().find(".group_m").hide(); 
        $(this).parent().find(".unfold").html("展开")
    }
})


function setSelectAll(q,x){
    allSelect=q.parent().parent().parent().prev().find("input");
    selectCount = 0;
    for(var i = 0; i < q.length; i++){
        if(q[i].checked == true){
            selectCount++;
        }
    }
    if(q.length == selectCount){
        allSelect[x].checked = true;
    }
    else{
        allSelect[x].checked = false;
    }
}  
//授权操作结束
//下载
$('.list-wrapper').on('click',".icon-download",function(){
	download($(this));
})
$(".list-header").on("click",'#downloadBtn',function(){
	download($(this));
})
//批量验权
function checkRight(obj,operate){
    right=false;
    id = findId(obj);
    if($(".list-group").parents("#home").length>0||$(".list-group").parents("#recycleDiv").length>0||$("#my-div").is(":visible")==true){
        right=true;
        return right;      
    }
    else{
        $.ajax({
            type: "POST",
            async: false,
            url:C.domain+"/share/index/check_operate",
            dataType:"jsonp",
            data: {
                "file_ids":id[1],
                "dir_ids":id[0],
                "want_operate":operate,
            },
            success: function(data){
                if(data.code == 0){
                    if(data.data.rightlessness.dir_ids.length==0&&data.data.rightlessness.file_ids.length==0){
                        right=true;
                        return right;
                    }
                    else{
                        var notitle=''
                        for(var i=0;i<data.data.rightlessness.dir_ids.length;i++){
                            notitle+=$("[dir_id="+data.data.rightlessness.dir_ids[i]+"]").find(".text a").html()+",";
                        }
                        for(var i=0;i<data.data.rightlessness.file_ids.length;i++){
                            notitle+=$("[file_id="+data.data.rightlessness.file_ids[i]+"]").find(".text a").html()+",";
                        }
                        Tips("<span>"+notitle+"</span>没有权限") 
                        right=false;
                        return right; 
                    }
                }
                else{
                    $('#myModal').modal('hide');
                    errorCode(data.code,data.msg)
                    right=false;
                    return right;
                }
            },
        })     
    }
}

//移动到
$(".list-header").on("click",'.move-to',function(){
    checkRight($(this),"rwd");
    if(right){
        move($(this));
    }
})
$('.list-wrapper').on('click','.move',function(){
    checkRight($(this),"rwd");
    if(right){
        move($(this));
    }
})
function move(obj){
    
    var treeContent;
	var fa_dir=[];
	fa_id=$("#main").attr("dir_id");
	fa_dir.push(fa_id);
	dir="dir";
	file="file";
	id = findId(obj);
	var arr_file="";
	var arr_dir="";
	if(id[1].length!== 0){
	arr_file = id[1].split(',')
	};
	if(id[0].length!== 0){
	arr_dir =id[0].split(',')
	}
    rootFolder=$(".right-col").attr("dir_id");
	var modalTitle = '移动到';
    if($(".right-col div:first-child").attr("id")=="home"||$("#my-div").is(":visible")==true){        
        treeContent = '<div class="file-tree-container"><ul class="treeview"><li><div class="treeview-node  treeview-root _minus" onclick="treeviewnode($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate minus"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ rootFolder + '">我的文件</span></span></div><ul class="treeview treeview-root-content treeview-content "></ul></li></ul></div>'; 
        folderTree();
    }else{          
        treeContent = '<div class="file-tree-container"><ul class="treeview"><li><div class="treeview-node2  treeview-root _minus" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate minus"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">共享文件</span></span></div><ul class="treeview treeview-root-content treeview-content "></ul></li></ul></div>'; 
        folderTree2();
    }
	$('#myModal').modal();
	$("#myModal #myModalLabel").html(modalTitle);
	$("#myModal .modal-body").html(treeContent);
	$('.modal-footer').html(modalFooter);    	
	$('.modal-footer').off('click').on('click','.tree_confirm',function(){
        $('#myModal').modal('hide');
        var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");
        if(targetID =="" || targetID ==undefined){            
            if($(".treeview-node-on").hasClass("treeview-node2")&&$(".treeview-node-on").hasClass("treeview-root")){
                Tips("不能复制移动到共享根目录");
            }else{
                Tips("请选择目标文件夹"); 
            }                        
        }else{
            //验目标文件是否有读写权限
            if($(".right-col div:first-child").attr("id")=="shareDiv"||$("#pub-div").is(":visible")==true){
                movetoRight(targetID,"dir","rw");
            }else{
                right=true;
            }
            if(right){	
                if($("#main").parent().attr("id")=="search"){
                    testchild=true;
                    arrf_dir =id[2].split(',');
                    var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");
                    for(var i=0;i<arrf_dir.length;i++){		
                        if(targetID===arrf_dir[i]){
                            $('#myModal').modal('hide');
                            Tips("非法操作：不能移动复制到当前或子文件夹下");
                            testchild=false;
                        } 
                    }
                }else if(id[0].length!== 0){			
                    childFolder(fa_dir,arr_dir);
                }else if(id[1].length!== 0 && id[0].length==0){		
                    var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");
                    if(targetID===fa_dir[0]){
                        $('#myModal').modal('hide');
                        Tips("非法操作：不能移动复制到当前或子文件夹下");
                        testchild=false;
                    }else{
                        testchild=true;
                    }
                }
                if(testchild){
                    var move_to = $(".treeview-node-on").children().find(".treeview-txt").attr("dir_id");            
                    urlType="move";		
                    txt='移动';
                    if($("#main").parent().attr("id")=="search"){
                        SingleMove.data1 = arr_file;
                        SingleMove.data2 = arr_dir;
                        //SingleMove.data3 = [];
                        //SingleMove.data4 = [];    
                        SingleMoveData.MoveTo = move_to
                        SingleMoveData.Action = urlType;
                        SingleMoveData.Status = 0;
                        SingleMove.RunL();
                    }else{		
                        moveCheck(urlType,id,move_to,txt);//验重复	
                    }
                        
                }
            }
            else{
                Tips("目标文件夹没有权限")
            }
        }
	})
}
//移动复制还原检测重名控件
function moveCheck(urlType,id,move_to,txt,same){   
    var url,
    data;
    if(txt=="复制"&&same==false){
       if($(".right-col>div:first").attr("id")=="home"||$("#my-div").is(":visible")==true){
            url=C.domain+"/share/move/copy_from_disk_check"
        }else if($(".right-col>div:first").attr("id")=="shareDiv"||$("#pub-div").is(":visible")==true){
            url=C.domain+"/disk/move/copy_from_share_check"
        }
        data={
            "share_file_ids":id[1],
            "share_dir_ids":id[0],
            "move_to":move_to
        }    
    }else{
        
        if($(".right-col>div:first").attr("id")=="home"){
            url=C.domain+"/disk/move/batch_check"
        }else{
            url=C.domain+"/share/move/batch_check"
        }
        data={
            "file_ids":id[1],
            "dir_ids":id[0],
            "move_to":move_to,
            "type":urlType
        }    
    }
	$.ajax({
		type: "POST",
		url:url,
		dataType:"jsonp",
		data:data,
		success: function(data){
			BatchMove.data = data.data;			
			if(data.code == 0){					
				BatchMoveData.MoveTo = move_to
				BatchMoveData.Action = urlType;
				BatchMoveData.Status = 0;
                BatchMoveData.Same = same;
				BatchMove.Run();						
			}else if(data.code == 180014){ //空间不够
				space();
				$('#myModal').modal('hide');
				Tips("当前操作需要"+data.data.use_format +"存储空间，剩余空间"+remnant+"，无法完成操作。");
			}else{
				$('#myModal').modal('hide');
				errorCode(data.code,data.msg)
			}
		},
	})
}

//我的文件复制到
$(".list-header").on("click",".copy-to",function(){
    if($(this).parents("#home").length>0||$("#my-div").is(":visible")==true){
        copy($(this));
    }
	else{
        copy2($(this));  
    }
})
$('.list-wrapper').on('click','.copy',function(){
    if($(this).parents("#home").length>0||$("#my-div").is(":visible")==true){
        copy($(this));
    }
	else{
        copy2($(this));  
    }
})
function copy(obj){
    rootFolder=$(".right-col").attr("dir_id");
var fa_dir=[];
	fa_id=$("#main").attr("dir_id");
	fa_dir.push(fa_id);
id = findId(obj);
	var arr_file="";
	var arr_dir="";
	if(id[1].length!== 0){
	arr_file = id[1].split(',')
	};
	if(id[0].length!== 0){
	arr_dir =id[0].split(',')
	}
var treeContent2 = '<div class="file-tree-container"><ul class="treeview"><li><div class="treeview-root _minus  treeview-node-on"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate minus"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">全部文件</span></span></div><ul class="treeview treeview-root-content treeview-content "><li><div class="treeview-node" onclick="treeviewnode($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ rootFolder + '">我的文件</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li><li><div class="treeview-node2 treeview-root" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">共享文件</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li></ul></li></ul></div>';
var modalTitle = '复制到';
$('#myModal').modal('show');
$("#myModal #myModalLabel").html(modalTitle);
$("#myModal .modal-body").html(treeContent2);

$('.modal-footer').html(modalFooter);
$('.modal-footer').off('click').on('click','.tree_confirm',function(){
    $('#myModal').modal('hide');
    var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");
    if(targetID =="" || targetID ==undefined){        
        if($(".treeview-node-on").hasClass("treeview-node2")&&$(".treeview-node-on").hasClass("treeview-root")){
            Tips("不能复制移动到共享根目录");
        }else{            
            Tips("请选择目标文件夹"); 
        } 
    }else{
        if($("#main").parent().attr("id")=="search"){
            testchild=true;
            arrf_dir =id[2].split(',');
            
            for(var i=0;i<arrf_dir.length;i++){		
                if(targetID===arrf_dir[i]){
                    $('#myModal').modal('hide');
                    Tips("非法操作：不能移动复制到当前或子文件夹下");
                    testchild=false;
                } 
            }
        }else if(id[0].length!== 0){			
                childFolder(fa_dir,arr_dir);
        }else if(id[1].length!== 0 && id[0].length==0){		
                var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");
                if(targetID===fa_dir[0]){
                    $('#myModal').modal('hide');
                    Tips("非法操作：不能移动复制到当前或子文件夹下");
                    testchild=false;
                }else{
                    testchild=true;
                }
        }
        if(testchild){
            var move_to = $(".treeview-node-on").children().find(".treeview-txt").attr("dir_id");         
            if($(".treeview-node-on").hasClass("treeview-node")){
                same=true;       
            }else{
                same=false;
                movetoRight(move_to,"dir","rw");
                if(!right){
                    Tips("目标文件夹没有权限")                    
                    return false;            
                }      
            } 
            urlType="copy";		
            txt='复制';
            if($("#main").parent().attr("id")=="search"){
                SingleMove.data1 = arr_file;
                SingleMove.data2 = arr_dir;
                //SingleMove.data3=[];
                //SingleMove.data4=[];
                SingleMoveData.MoveTo = move_to
                SingleMoveData.Action = urlType;
                SingleMoveData.Status = 0;
                SingleMoveData.Same = same;
                SingleMove.RunL();
            }else{
                moveCheck(urlType,id,move_to,txt,same);//验重复
            }
        }
}
})
}
function copy2(obj){
var fa_dir=[];
	fa_id=$("#main").attr("dir_id");
	fa_dir.push(fa_id);
id = findId(obj);
	var arr_file="";
	var arr_dir="";
	if(id[1].length!== 0){
	arr_file = id[1].split(',')
	};
	if(id[0].length!== 0){
	arr_dir =id[0].split(',')
	}
 rootFolder=$(".right-col").attr("dir_id");
var treeContent2 = '<div class="file-tree-container"><ul class="treeview"><li><div class="treeview-root _minus  treeview-node-on"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate minus"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">全部文件</span></span></div><ul class="treeview treeview-root-content treeview-content "><li><div class="treeview-node" onclick="treeviewnode($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ rootFolder + '">我的文件</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li><li><div class="treeview-node2 treeview-root" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">共享文件</span></span></div><ul class="treeview treeview-content treeview-collapse"></ul></li></ul></li></ul></div>';
var modalTitle = '复制到';
$('#myModal').modal('show');
$("#myModal #myModalLabel").html(modalTitle);
$("#myModal .modal-body").html(treeContent2);

$('.modal-footer').html(modalFooter);
//folderTree2();
$('.modal-footer').off('click').on('click','.tree_confirm',function(){
    $('#myModal').modal('hide');
    var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");   
    if(targetID =="" || targetID ==undefined){        
        if($(".treeview-node-on").hasClass("treeview-node2")&&$(".treeview-node-on").hasClass("treeview-root")){
            Tips("不能复制移动到共享根目录");
        }else{            
            Tips("请选择目标文件夹"); 
        }
    }else{
        if($("#main").parent().attr("id")=="search"){
            testchild=true;
            arrf_dir =id[2].split(',');
            
            for(var i=0;i<arrf_dir.length;i++){		
                if(targetID===arrf_dir[i]){
                    $('#myModal').modal('hide');
                    Tips("非法操作：不能移动复制到当前或子文件夹下");
                    testchild=false;
                } 
            }
        }else if(id[0].length!== 0){
           
                childFolder(fa_dir,arr_dir);
        }else if(id[1].length!== 0 && id[0].length==0){
                var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");
                if(targetID===fa_dir[0]){
                    $('#myModal').modal('hide');
                    Tips("非法操作：不能移动复制到当前或子文件夹下");
                    testchild=false;
                }else{
                    testchild=true;
                }
        }
        if(testchild){
            var move_to = $(".treeview-node-on").children().find(".treeview-txt").attr("dir_id"); 
            if($(".treeview-node-on").hasClass("treeview-node")){
                same=false;            
            }else{
                same=true; 
                movetoRight(move_to,"dir","rw");
                if(!right){
                    Tips("目标文件夹没有权限")                    
                    return false;            
                }
            }
            urlType="copy";		
            txt='复制';
            if($("#main").parent().attr("id")=="search"){
                SingleMove.data1 = arr_file;
                SingleMove.data2 = arr_dir;
                //SingleMove.data3 = [];
               // SingleMove.data4 = [];
                SingleMoveData.MoveTo = move_to
                    SingleMoveData.Action = urlType;
                    SingleMoveData.Same = same;
                    SingleMoveData.Status = 0;
                    SingleMove.RunL();
            }else{
                moveCheck(urlType,id,move_to,txt,same);//验重复
            }
            }
}
})
}
//清空回收站
$(".recyclebtn").click(function(){
var modalTitle = '清空回收站';
$('#myModal').modal();
$(".modal-title").html(modalTitle);
$(".modal-body").html('<p class="simple-txt">确认清空回收站？</p>');
$(".modal-footer").html(modalFooter);
$('.modal-footer').off('click').on('click','.btn-primary',function(){
 var url;   
 if($(".right-col>div:first").attr("id")=="recycleDiv"){
   url=C.domain+"/disk/dir/clear_trash";
}else{
        var file_ids='';
        var dir_ids='';
       $("[file_id]").each(function(){
           if(file_ids ==""){
					file_ids +=$(this).attr("file_id");
				}
				else{
					file_ids += ","+ $(this).attr("file_id");
				}
       
        }) 
        $("[dir_id]").each(function(){
           if(dir_ids ==""){
					dir_ids +=$(this).attr("dir_id");
				}
				else{
					dir_ids += ","+ $(this).attr("dir_id");
				}
        }) 
        $.ajax({
            type: "POST",
            async: false,
            url:C.domain+"/share/index/check_operate",
            dataType:"jsonp",
            data: {
                "file_ids":file_ids,
                "dir_ids":dir_ids,
                "want_operate":"rwd",
            },
            success: function(data){
                if(data.code == 0){
                        if(data.data.rightlessness.dir_ids.length==0&&data.data.rightlessness.file_ids.length==0){
                           url=C.domain+"/share/dir/clear_trash"; 
                           return;
                        }else{
                           var notitle=''
                           for(var i=0;i<data.data.rightlessness.dir_ids.length;i++){
                               notitle+=$("[dir_id="+data.data.rightlessness.dir_ids[i]+"]").find(".text a").html()+",";
                           }
                            for(var i=0;i<data.data.rightlessness.file_ids.length;i++){
                               notitle+=$("[dir_id="+data.data.rightlessness.file_ids[i]+"]").find(".text a").html()+",";
                           }
                           Tips("<span>"+notitle+"</span>没有权限")                           
                           return false; 
                        }
                }else{
                    $('#myModal').modal('hide');
                    errorCode(data.code,data.msg)
                    return false; 
                }
            },
        })
    
}
	$.ajax({
		type: "POST",
		url: url,
		dataType:"jsonp",
		success: function(data){
				if(data.code == 0){
					$('#myModal').modal('hide');					
					$('.list-group').html('');
					firstCheck();					
					$(".check-icon").mask();
                     if($(".right-col>div:first").attr("id")=="recycleDiv"){
                        space();
                        }
					TipsSuccess("回收站已清空");
                    
				}else{
					errorCode(data.code,data.msg);
					}
				},
		});
	});
});
//回收站删除
$(".list-group").on("click",".icon-delete",function(){
    recycleDelete($(this));
})
//共享回收站删除
$("#pubRecycleDiv").off("click").on("click",".icon-delete",function(){
    checkRight($(this),"rwd");
    if(right){
        recycleDelete($(this));
    }
})
function recycleDelete(obj){
id = findId(obj);
	var modalTitle = '彻底删除';
	$('#myModal').modal();
	$(".modal-title").html(modalTitle);
	$(".modal-body").html('<p class="simple-txt">文件删除后将无法恢复，您确认要彻底删除所选文件吗？</p>');
	$(".modal-footer").html(modalFooter);
	$('.modal-footer').off('click').on('click','.btn-primary',function(){
    var url;
    if($(".right-col>div:first").attr("id")=="recycleDiv"){
       url=C.domain+"/disk/dir/batch_delete_trash";
    }else{
       url=C.domain+"/share/dir/batch_delete_trash";  
    }
		$.ajax({
			type: "POST",
			url:url,
			dataType:"jsonp",
			data: {"file_ids":id[1],"dir_ids":id[0]},
			success: function(data){
				if(data.code == 0){
					$('#myModal').modal('hide');					
					removeList(id[0],id[1]);
					TipsSuccess("文件删除成功!");
					space();
				}else{
					errorCode(data.code,data.msg);
					}
				},
		});
});
};
//还原
$('#back').on('click',function(){
    if($(this).parents("#recycleDiv").length>0){
        fileBack($(this));
    }else{
        checkRight($(this),"rwd")
        if(right){
            pubFileBack($(this));
        }
    }    
})
$('#recycleDiv').off('click').on('click','.icon-back',function(){
        fileBack($(this));
})
//共享回收站还原
$('#pubRecycleDiv').off('click').on('click','.icon-back',function(){
    checkRight($(this),"rwd")
    if(right){
        pubFileBack($(this));
    }
})
function fileBack(obj){
 rootFolder=$(".right-col").attr("dir_id");
var treeContent = '<div class="file-tree-container"><ul class="treeview"><li><div class="treeview-node  treeview-root _minus" onclick="treeviewnode($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate minus"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="'+ rootFolder + '">全部文件</span></span></div><ul class="treeview treeview-root-content treeview-content "></ul></li></ul></div>';
id = findId(obj);
var arr_file=[]
var arr_dir=[];
if(id[1].length!== 0){
arr_file = id[1].split(',')
};
if(id[0].length!== 0){
arr_dir = id[0].split(',')
};

idSum = arr_file.length + arr_dir.length;
listSum = $(".list").length;
	var modalTitle = '还原到';
	$('#myModal').modal('show');
	$(".modal-title").html(modalTitle);
	$(".modal-body").html(treeContent);
	$(".modal-footer").html(modalFooter);
	folderTree();
	$('.modal-footer').off('click').on('click','.tree_confirm',function(){
        $('#myModal').modal('hide');
	var move_to = $(".treeview-node-on").children().find(".treeview-txt").attr("dir_id");//目标文件夹id	
	
if(move_to =="" || move_to ==undefined){
	
	Tips("请选择目标文件夹")
}else{
	urlType="move";
		txt='还原';
		SingleMove.data1 = arr_file;	
		SingleMove.data2 = arr_dir;
        //SingleMove.data3 = [];
        //SingleMove.data4 = [];
		SingleMoveData.MoveTo = move_to
		SingleMoveData.Action = urlType;
		SingleMoveData.Status = 0;
		SingleMove.RunL();
}	
});
}
//共享还原
function pubFileBack(obj){
treeContent = '<div class="file-tree-container"><ul class="treeview"><li><div class="treeview-node2  treeview-root _minus" onclick="treeviewnode2($(this))"><span class="treeview-node-handler"><em class="b-in-blk plus icon-operate minus"></em><dfn class="b-in-blk treeview-ic"></dfn><span class="treeview-txt" dir_id="">共享文件</span></span></div><ul class="treeview treeview-root-content treeview-content "></ul></li></ul></div>'; 
    id = findId(obj);
    var arr_file=[]
    var arr_dir=[];
    if(id[1].length!== 0){
        arr_file = id[1].split(',')
    };
    if(id[0].length!== 0){
        arr_dir = id[0].split(',')
    };
    
    var move_to;
    urlType="move";
    txt='还原';
    SingleMoveData.Action = urlType;
    SingleMoveData.Status = 0;
   
    //检查是否有原始路径
    $.ajax({
		type: "POST",
		url: C.domain+"/share/dir/source_path",
		dataType:"jsonp",
		data: {"file_ids":id[1],"dir_ids":id[0]},
		success: function(data){
            if(data.code == 0){
                if(data.data.exist.dir_ids.length>0||data.data.exist.file_ids.length>0){
                     //有原始路径                     
                    SingleMove.data5=[];
                    SingleMove.data6=[];                       
                    SingleMove.data4=data.data.exist.dir_ids;                            
                    SingleMove.data3=data.data.exist.file_ids;                            
                    SingleMove.RunP();
            }else if(data.data.not_found.dir_ids.length>0||data.data.not_found.file_ids.length>0){
                //没有原始路径
                var modalTitle = '还原到';
                $('#myModal').modal('show');
                $(".modal-title").html(modalTitle);
                $(".modal-body").html(treeContent);
                $(".modal-footer").html(modalFooter);
                folderTree2();
                $('.modal-footer').off('click').on('click','.tree_confirm',function(){
                    $('#myModal').modal('hide');
                    move_to = $(".treeview-node-on").children().find(".treeview-txt").attr("dir_id");//目标文件夹id	
                    if(move_to =="" || move_to ==undefined){
                        Tips("请选择目标文件夹")
                    }else{
                        movetoRight(move_to,"dir","rwd");
                        if(right){                
                            SingleMove.data5 = data.data.not_found.file_ids;
                            SingleMove.data6 = data.data.not_found.dir_ids;
                            SingleMove.data3=[];
                            SingleMove.data4=[];
                            SingleMoveData.MoveTo = move_to
                            SingleMoveData.Action = urlType;
                            SingleMoveData.Status = 0;
                            SingleMove.RunP(); 
                        }else{
                             Tips("目标文件夹没有权限")
                        }
                    }	
                });
            }
            
		}else{
                errorCode(data.code,data.msg);
            }
        }
	});

    /*/没有原始路径执行
	var modalTitle = '还原到';
	$('#myModal').modal('show');
	$(".modal-title").html(modalTitle);
	$(".modal-body").html(treeContent);
	$(".modal-footer").html(modalFooter);
	folderTree2();
	$('.modal-footer').off('click').on('click','.tree_confirm',function(){
        $('#myModal').modal('hide');
        move_to = $(".treeview-node-on").children().find(".treeview-txt").attr("dir_id");//目标文件夹id	
        if(move_to =="" || move_to ==undefined){
            
            Tips("请选择目标文件夹")
        }else{
            movetoRight(move_to,"dir","rwd");
            if(right){                
                SingleMove.data1 = arr_file;	
                SingleMove.data2 = arr_dir;
                SingleMove.data3=[];
                SingleMove.data4=[];
                SingleMoveData.MoveTo = move_to
                SingleMoveData.Action = urlType;
                SingleMoveData.Status = 0;
                SingleMove.RunL(); 
            }else{
                 Tips("目标文件夹没有权限")
            }
        }	
    });
*/
}

               
//点击文件名
$(".container").off("click").on("click", ".filename,.grid-view .filetype", function(){
	name=$(this).html();
	if(name==""){
		name=$(this).closest(".list").find(".filename").html();
	}
	clickName($(this),name)
	
})
//点击上传文件夹
$("#upModal .file-path").off("click").on("click", ".filename", function(){
	dir_id=$(this).attr("dir_id");
	name=$(this).html();
	//name= name[name.length-1];
	$(".nav-pills li").removeClass("active");
	$(".nav-pills li:first-child").addClass("active");
	$(".right-col").html("");
	$(".right-col").load("home.html");
	setTimeout(function(){		
		openDir(dir_id,name);
	},500)
})
function clickName(obj,name){
    if(obj.closest(".list").attr("file_id")){	
		download(obj);
	}
    else{        
		if(obj.attr("dir_id")){
			dir_id = obj.attr("dir_id");            
		}
		else{
			dir_id=obj.closest(".list").attr("dir_id");
        }
        if(obj.parents("#shareDiv").length>0&&obj.attr("dir_id")==""){           
           window.location.href=C.domain+"/share/index/view";
        }else{            
            openDir(dir_id,name)
        }		
    }	
}

//共享文件夹点击
$('.list-wrapper').on('click','.sharename',function(){
var id = findId($(this));
var uname = $("#main").attr("uname");
if(typeof($(this).closest(".list").attr("dir_id"))=="undefined"){//是文件
	download($(this));
}else{//是文件夹
		$(".list-wrapper").html("");
        openDir(id[0],uname)
}
})
});

//验证是否子文件夹
function childFolder(fa_dir,id){
	testchild=true;
	var targetID =$(".treeview-node-on").find(".treeview-txt").attr("dir_id");

		for(var i=0;i<id.length;i++){		
		if(targetID===id[i] || targetID===fa_dir[i]){
			$('#myModal').modal('hide');
			Tips("非法操作：不能移动复制到当前或子文件夹下");
			testchild=false;
		} 
		$(".treeview-node-on").parents(".treeview").each(function(){			
		  if($(this).prev().find(".treeview-txt").attr("dir_id")===id[i]){	
		  $('#myModal').modal('hide');
		  Tips("非法操作：不能移动复制到当前或子文件夹下");
		  testchild=false;
		  } 
		})
	}
}


//点击搜索路径
$(".search").off("click").on("click",".file-path",function(){
	dir_id=$(this).attr("dir_id");
	var name = $(this).text().split("\\");
	name= name[name.length-1];
    if($(this).parents('#my-div').length>0){
        window.location.href="/disk/index/view?id="+dir_id+"&name="+name;
    }else if($(this).parents('#pub-div').length>0&& dir_id==""){
        window.location.href="/share/index/view";
    }else{
        window.location.href="/share/index/view?id="+dir_id+"&name="+name;
    }
})

//--------------------缩略图居中显示-------------------------
  function drawpic(dom){    
    var _img=$(dom);
    var imgW=_img.width();
	var imgH=_img.height();
	l=(imgW-92)/2;
	t=(imgH-92)/2;
    var c=(_img.width()/2);    
    _img.css({"position":"absolute","left":-l+"px","top":-t+"px"}); 
 }
}
//清除复选框内容变为不可操作
function noCheck(){
	$(".list-view .list-header ul").show();
	$(".list-header-operatearea").hide();
	$(".list-view-container").removeClass("list-multselected");
    $(".list-header .check").removeClass("checked");
	$(".list").each(function(){
		$(this).removeClass("list-selected");
	})
}
//打开文件夹
function openDir(dir_id,name){
var urlNav;
var url;
if($(".right-col>div:first").attr("id")=="home"){
   urlNav=C.domain+"/disk/file/get_nav";
}else if($(".right-col>div:first").attr("id")=="shareDiv"){
   urlNav=C.domain+"/share/file/get_nav";  
}
//面包屑导航
	$.ajax({
		type: "POST",
		url: urlNav,
		dataType:"jsonp",
		data:{"dir_id":dir_id},
		success: function(data){
			if(data.code == 0){
				var navli=[];
				var i = "";
				for( i in data.data.nav){
					var std = data.data.nav[i]
					navli+='<li><a href="javascript:void(0);" class="filename" dir_id="'+ std.id +'">'+ std.name +'</a></li>';
				}	
				navli+='<li class="active">'+ name +'</li>'	
				$(".breadcrumb").html(navli);               
			}else{
				errorCode(data.code,data.msg);
				}
				},
	});
	
	$("#main").attr("dir_id",dir_id);
	$(".list-header-operatearea").hide();
	$(".list-view-container").removeClass("list-multselected");
	$(".list-wrapper").html("");
	firstCheck();
	var id;
	id= $("#main").parent().attr("id");	
		switch(id)
		{
		case "home":
			listTemplate = $("#listTemplate");
			url = url1;			
		break;

		case "shareDiv":
			listTemplate = $("#publicShareListTemplate");
			url = url3;
		break;		
		}
		params={
			page:1,
			dir_id:dir_id,
			limit: 100
			};
		
		getList(url,params,listTemplate);                      
}
//初始复选框
function firstCheck(){
	$(".check").removeClass("checked");
	$(".list-view .list-header ul").show();
	$(".list-header-operatearea").hide();
}

//获取url参数
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     //r=unescape(r);
     if(r!=null)return   decodeURI(r[2]); return null;
}
//--------------时间转换---------------
function newTime(oldtime){
	return oldtime;
date = new Date(oldtime*1000)
Y = date.getFullYear();
M = (date.getMonth()+1 < 10 ? '0' +(date.getMonth()+1) : date.getMonth()+1);
D = date.getDate() < 10 ? '0' + date.getDate() : + date.getDate();
h = date.getHours() < 10 ? '0' + date.getHours() : + date.getHours();
m = date.getMinutes() < 10 ? '0' + date.getMinutes() : + date.getMinutes();
return(Y+'-'+M+'-'+D+' '+h+':'+m); 
}

//-------------- 屏蔽插件 begin ------------------
(function ($) {
//屏蔽，适合单个元素.
$.fn.amask = function () {
var divHtml = '<div class="adivMask" style="position: absolute; width:100%;z-index:10; height:100%;margin-top:0; top: 0px; opacity: 0; filter: alpha(opacity=0)"> </div>';
$(this).wrap('<div style="position:relative"></div>');
$(this).parent().append(divHtml);
$(this).data("amask","true");
}
//取消屏蔽
$.fn.unamask = function () {
if($(this).next().hasClass("adivMask")){
$(this).parent().find(".adivMask").remove();
$(this).unwrap();
$(this).data("amask", "false");
}
}
})(jQuery);
//-------------------文件大小格式化------------------
function FormatSize(size){
        G = 1073741824;
        M = 1048576;
        format = "";
        if(size > G){
            format = (size / G).toFixed(2) + "G";
        }else if(size > M){
            format =(size / M).toFixed(2) +"M";
        }else{
            format = (size / (M / 1024)).toFixed(2) +"K";
        }
        return format;
    }
//-------------------用户组弹层------------------
$(".add-group").click(function(){
   $(".file-tree-container").html("");
   $("#groupModal .modal-footer p").remove();
    //$.ajax({
	//	type: "POST",
	//	url: '',//用户组列表地址
	//	dataType:"jsonp",
	//	data:{"page":page},
	//	success: function(data){
	//		if(data.code == 0){
				var showlist = $("<ul class='treeview clear'></ul>");
                showall(data.grouplist, showlist);
                $(".file-tree-container").append(showlist);
                $("#groupModal").modal(); 
	//		}else{
	//			errorCode(data.code,data.msg);
	//			}
	//			},
	//});
      
})
 
$("#groupModal .btn-primary").click(function(){
    var l=$(".treeview-sel");
    if(l.length<=0){
        $("#groupModal .modal-footer").append('<p class="red" style="padding-top:8px">尚未选择用户组</p>');
        $(".add-group").next(".red").html("请选择所在用户组")
        $(".selgroup").remove();
        return false;
    }else{
        $("#groupModal .modal-footer p").remove()
        $(".add-group").next(".red").html("")        
    }
    $("#groupModal").modal("hide");   
    if(l.length>0&&$(".selgroup").length<=0){
    $(".add-group").parent().parent().append("<div class='selgroup'><b>所选用户组：</b><div class='selgrouplist'></div></div>")    
    }else{
        $(".selgrouplist").html('')
    }
    for(var i=0;i<l.length;i++){
        var gid=l.eq(i).attr("gid");
        var grouplist='';
        var list=l.eq(i).parents(".treeview").prev();
        for(var j=list.length-1;j>=0;j--){
            _this=list.eq(j);
            grouplist+=_this.html()+"->";
            console.log(grouplist);
        }
        grouplist+=l.eq(i).html();
        $(".selgrouplist").append("<p gid='"+gid+"'>"+grouplist+"<input name='group_ids' value='"+gid+"' style='display:none'></input></p>");
    }
  
})
function showall(menu_list, parent) {
    var gid,Name,ctime,modify,depth;
    for (var i in menu_list){
        gid=menu_list[i].gid;//定义用户组id
        Name=menu_list[i].Name;//定义用户组名称
        ctime=menu_list[i].ctime;//定义用户组修改时间
        modify= menu_list[i].modify;//定义用户组修改时间
        depth=menu_list[i].depth;//定义用户组深度
        //如果有子节点，则遍历该子节点
        if (menu_list[i].child.length > 0) {
            //创建一个子节点li
            var li = $("<li class='clear'></li>");
            if($("#groupModal").find(".file-tree-container").length>0){
                group_list='<em class="b-in-blk plus icon-operate"  title="点击展开" ></em><span class="treeview-txt" gid="'+gid+'">'+Name+'</span>'
            }else{
                 group_list='<div class="group_list"><div class="treeview-node-handler group-l1"><em class="b-in-blk plus icon-operate"  title="点击展开"></em><span class="treeview-txt">'+Name+'</span></div><div class="group-l2">'+ctime+'</div><div class="group-l3">'+modify+'</div><div class="group-l4" depth="'+depth+'"><a href="/group/info?pkid='+gid+'">编辑</a>  <a href="/group/info?pkid='+gid+'">添加</a> <a href="javascript:void(0);" gid="'+gid+'" onclick="deleteG(this)">删除</a></div></div>'
            }
            //将li的文本设置好，并马上添加一个空白的ul子节点，并且将这个li添加到父亲节点中
            var add="<em></em>"
            $(li).append(group_list).before("<em>*</em>").append("<ul class='treeview treeview-collapse clear'></ul>").appendTo(parent);           
            
            //将空白的ul作为下一个递归遍历的父亲节点传入
            showall(menu_list[i].child, $(li).children().last());
        }
        //如果该节点没有子节点，则直接将该节点li以及文本创建好直接添加到父亲节点中
        else {
            if($("#groupModal").find(".file-tree-container").length>0){
                group_list='<span class="treeview-txt" gid="'+gid+'">'+Name+'</span>'
            }else{
                 group_list='<div class="group_list"><div class="treeview-node-handler group-l1"><span class="treeview-txt">'+Name+'</span></div><div class="group-l2">'+ctime+'</div><div class="group-l3">'+modify+'</div><div class="group-l4"  depth="'+depth+'"><a href="/group/info?pkid='+gid+'">编辑</a>  <a href="/group/info?pkid='+gid+'">添加</a>  <a href="javascript:void(0);" gid="'+gid+'" onclick="deleteG(this)">删除</a></div></div>'
            }
            
            $("<li class='clear'></li>").append(group_list).appendTo(parent);
        }
    }
    //$("#groupModal").hideLoading();
}
function showallUser(menu_list, parent) {
    var pkid,name,uid,operate;
    var i=0;
    for (i in menu_list){
        //如果有子节点，则遍历该子节点
        pkid=menu_list[i].pkid;//定义用户组id
        name=menu_list[i].Name;//定义用户组名称
        //user_list=menu_list[i].ctime;//定义用户组修改时间
        //uid= menu_list[i].user_list[i].uid;//定义用户组修改时间
        operate=menu_list[i].operate;//定义用户组深度
        var users=menu_list[i].user_list;      
        var u_list=''; 
        
        if (menu_list[i].user_list.length > 0) {
            for(var j=0;j< menu_list[i].user_list.length;j++){
               
                var u=menu_list[i].user_list[j];
                u_list+='<div uid="'+u.unique_id+'" operate="'+u.operate+'"><span>'+u.name+'</span><label><input name="r" type="checkbox">[R]</label><label><input name="w" type="checkbox">[W]</label><label><input name="d" type="checkbox">[A]</label></div>' 
                
            }
            u_list='<div class="subgroup clear"><div class="allselect">全选<label><input value="checkbox" name="ar" type="checkbox">[R] </label><label><input name="aw" type="checkbox">[W] </label><label><input name="ad" type="checkbox">[A] </label></div><div class="sub clear">'+u_list+'</div>'
           
        }else{
            u_list=''; 
        }
        if (menu_list[i].child.length > 0) {
                
                //创建一个子节点li
                var li = $("<div class='group clear'></div>");            
                     g_list='<a href="javascript:void(0);" class="unfold">展开</a><div class="right_title"><p>'+name+'</p><div class="d_right" operate="'+operate+'">（ 默认权限<label><input value="checkbox" name="r" type="checkbox">[R]</label><label><input value="checkbox" name="w"type="checkbox">[W]</label><label><input value="checkbox" name="d" type="checkbox">[A]</label> )</div></div>'+u_list+'';
               
                //将li的文本设置好，并马上添加一个空白的ul子节点，并且将这个li添加到父亲节点中            
                $(li).append(g_list).append( '<div class="group_m"></div>').appendTo(parent);           
                
                //将空白的ul作为下一个递归遍历的父亲节点传入
                showallUser(menu_list[i].child, $(li).children().last());
            }
            //如果该节点没有子节点，则直接将该节点li以及文本创建好直接添加到父亲节点中
        else {
            if(u_list==null||u_list==""){
                var li = $("<div class='group clear'></div>"); 
            g_list='<div class="right_title"><p>'+name+'</p><div class="d_right" operate="'+operate+'">（ 默认权限<label><input value="checkbox" name="r" type="checkbox">[R]</label><label><input value="checkbox" name="w"type="checkbox">[W]</label><label><input value="checkbox" name="d" type="checkbox">[A]</label> )</div></div>';
            }else{
                var li = $("<div class='group clear'></div>"); 
            g_list='<a href="javascript:void(0);" class="unfold">展开</a><div class="right_title"><p>'+name+'</p><div class="d_right" operate="'+operate+'">（ 默认权限<label><input value="checkbox" name="r" type="checkbox">[R]</label><label><input value="checkbox" name="w"type="checkbox">[W]</label><label><input value="checkbox" name="d" type="checkbox">[A]</label> )</div></div>'+u_list+'';
            }
            
            $(li).append(g_list).appendTo(parent);
            //break;
        }
        
    }
    //$("#groupModal").hideLoading();
}
//验证是否可以删除用户组
function deleteG(obj){
    gid=$(obj).attr("gid");
    $.ajax({
        type: "POST",
        dataType : "jsonp",
        url:'',//验证是否有子用户组或者用户地址
        data:{
             "gid":gid
            },
        success:function(data){           
            if(data.code==0){
            //如果没有子用户或者用户组  
                $('#myModal').modal();
                $(".modal-title").html("确认删除");
                $(".modal-body").html('<p class="simple-txt">确认删除'+ +'用户组？</p>');
                $(".modal-footer").html(modalFooter);
                $('.modal-footer').off('click').on('click','.btn-primary',function(){
                   deleteGroup(gid);
                })            
            }
            else{
                errorCode(data.code,data.msg);
            }                                     
        }
    })
}
//删除用户组
 function deleteGroup(id){
    $.ajax({
        type: "POST",
        dataType : "jsonp",
        url:'',//删除用户组地址
        data:{
             "id":id
            },
        success:function(data){           
            if(data.code==0){
                TipsSuccess("删除成功！")         
            }
            else{
                errorCode(data.code,data.msg);
            }                                     
        }
    });
     
 }   